package com.lph.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.lph.model.Enterprise;
import com.lph.model.Files;
import com.lph.model.MenProperties;
import com.lph.model.Menber;
import com.lph.model.Msg;
import com.lph.service.BaseServiceI;
import com.lph.util.ExcelUtil;
import com.lph.util.FileIOUtil;
import com.lph.util.MD5Util;

@Controller
@RequestMapping("/mc")
public class MenberController extends BaseController<Menber>{
	@Resource(name="menberService")
	private BaseServiceI<Menber> bsi;
	
	@Resource(name="filesService")
	private BaseServiceI<Files> fsi;
	
	@Resource(name="enterpriseService")
	private BaseServiceI<Enterprise> ebsi;
	
	@Override
	public List<Menber> find(Menber t) {
		return bsi.find(t);
	}
	@RequestMapping("/index.coc")
	public String index(){
		return "/menber/index";
	}
	
	@RequestMapping("/addmenber")
	@ResponseBody
	public Msg insert(Menber t,
			@RequestParam(required=false,value="photo") MultipartFile[] files,HttpServletRequest req) {
		//验证短信验证码
		HttpSession session=req.getSession();
		String codePwd=session.getAttribute("codePwd")+"";
		System.out.println(t.getCode());
		if(!(t.getMobile()+"-"+t.getCode()).equals(codePwd)){
			return new Msg("手机验证码错误!请重新获取,在提交保存",false);
		}
		
		
		Msg m=bsi.insert(t);
		if(m.isIs()){
			try {
				List<Files> list=FileIOUtil.saveFile(files,"t_coc_menber",t.getId());
				if(list!=null){
					fsi.insert(list.get(0));
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return m;
	}
	
	@RequestMapping("/aoe.coc")
	public String  addOrEdit(HttpServletRequest req){
		String vals=req.getParameter("vals");
		if(vals!=null){
			try {//涓枃涔辩爜
				vals=new String(vals.getBytes("iso-8859-1"),"utf-8");
			} catch (Exception e) {
				e.printStackTrace();
			}
		//鎶婂瓧绗︿覆杞崲涓簀son瀵硅薄
		Menber mb=JSON.parseObject(vals,Menber.class);
		req.setAttribute("mb", mb);
		}
		return "/menber/addOrEdit";
		
	}
	@Override
	public Msg edit(Menber t, MultipartFile[] files) {
		return bsi.edit(t);
	}
	
	@Override
	public Msg remove(Integer id) {
		return bsi.remove(id);
	}
	
	@Override
	public Msg batchRemove(String ids) {
		return bsi.batchRemove(ids);
	}
	
	@RequestMapping("/showPro.coc")
	public String showPro(HttpServletRequest req) throws Exception{
		String mps=req.getParameter("mps");
		if(mps!=null){
			mps=new String(mps.getBytes("iso-8859-1"),"utf-8");
			List<MenProperties> list=JSON.parseArray(mps,MenProperties.class);
			//璁剧疆request缂栫爜鏍煎紡
			
				req.setCharacterEncoding("utf-8");
			
			req.setAttribute("list", list);
		}
		return "menber/showPor";
		
	}
	@RequestMapping("/enterprise.coc")
	public String enterPrise(HttpServletRequest req){
		String menId=req.getParameter("menId");
		Enterprise t=new Enterprise();
		t.setMenId(Integer.parseInt(menId));
		
		List<Enterprise>list =ebsi.find(t);
		req.setAttribute("list", list);
		req.setAttribute("menId", menId);
		return "/menber/enterprise";
		
	}
	
	@RequestMapping("/editPwd.coc")
	@ResponseBody
	public Msg editPwd(Menber m){
		String password=MD5Util.getMd5(m.getPassword());

				m=bsi.get(m);
		if(m==null){
			return new Msg("鐢ㄦ埛涓嶅瓨鍦�,瀵嗙爜淇敼澶辫触",false);
		}else{
			m.setPassword(password);
			return bsi.edit(m);
		}
		
	}
	@Override
	public Msg outExcle(String fileName, String datas) {
		Msg m = new Msg("瀵煎嚭澶辫触",false);
		List<Menber> list = JSON.parseArray(datas, Menber.class);
		try {
			ExcelUtil.out(list,fileName);
			m = new Msg("瀵煎嚭鎴愬姛",true);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return m;
	}
}
